package com.atguigu.daijia.map.controller;

import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.map.service.LocationService;
import com.atguigu.daijia.model.form.map.OrderServiceLocationForm;
import com.atguigu.daijia.model.form.map.SearchNearByDriverForm;
import com.atguigu.daijia.model.form.map.UpdateDriverLocationForm;
import com.atguigu.daijia.model.form.map.UpdateOrderLocationForm;
import com.atguigu.daijia.model.vo.map.NearByDriverVo;
import com.atguigu.daijia.model.vo.map.OrderLocationVo;
import com.atguigu.daijia.model.vo.map.OrderServiceLastLocationVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Tag(name = "位置API接口管理")
@RestController
@RequestMapping("/map/location")
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class LocationController {

    private final LocationService locationService;

    /**
     * 司机开始接单，更新司机位置信息
     * @param updateDriverLocationForm
     * @return
     */
    @Operation(summary = "司机开始接单，更新司机位置信息")
    @PostMapping("/updateDriverLocation")
    public Result<Boolean> updateDriverLocation(@RequestBody UpdateDriverLocationForm updateDriverLocationForm) {
        Boolean result = locationService.updateDriverLocation(updateDriverLocationForm);
        return Result.ok(result);
    }

    /**
     * 司机停止接单，删除司机位置信息
     * @param driverId
     * @return
     */
    @Operation(summary = "司机停止接单，删除司机位置信息")
    @DeleteMapping("/removeDriverLocation/{driverId}")
    public Result<Boolean> removeDriverLocation(@PathVariable("driverId") Long driverId) {
        Boolean result = locationService.removeDriverLocation(driverId);
        return Result.ok(result);
    }

    /**
     * 查询附近适合接单的司机
     * @param searchNearByDriverForm
     * @return
     */
    @Operation(summary = "查询附近适合接单的司机")
    @PostMapping("/searchNearByDriver")
    public Result<List<NearByDriverVo>> searchNearByDriver(@RequestBody SearchNearByDriverForm searchNearByDriverForm) {
        List<NearByDriverVo> list = locationService.searchNearByDriver(searchNearByDriverForm);
        return Result.ok(list);
    }

    /**
     * 接单司机前往代驾订单起始点过程中更新司机位置信息到缓存
     * @param updateOrderLocationForm
     * @return
     */
    @Operation(summary = "接单司机前往代驾订单起始点过程中更新司机位置信息到缓存")
    @PostMapping("/updateOrderLocationToCache")
    public Result<Boolean> updateDriverLocationToCache(@RequestBody UpdateOrderLocationForm updateOrderLocationForm) {
        Boolean result = locationService.updateDriverLocationToCache(updateOrderLocationForm);
        return Result.ok(result);
    }

    /**
     * 从缓存中获取接单司机前往代驾订单起始点过程中的位置信息
     * @param orderId
     * @return
     */
    @Operation(summary = "从缓存中获取接单司机前往代驾订单起始点过程中的位置信息")
    @GetMapping("/getCacheOrderLocation/{orderId}")
    public Result<OrderLocationVo> getDriverLocationFromCache(@PathVariable("orderId") Long orderId) {
        OrderLocationVo orderLocationVo = locationService.getDriverLocationFromCache(orderId);
        return Result.ok(orderLocationVo);
    }

    /**
     * 开始代驾服务后，批量保存订单服务过程中司机位置信息
     * @param orderServiceLocationFormList
     * @return
     */
    @Operation(summary = "开始代驾服务后，批量保存订单服务过程中司机位置信息")
    @PostMapping("/saveOrderServiceLocation")
    public Result<Boolean> batchSaveLocationDuringService(@RequestBody List<OrderServiceLocationForm> orderServiceLocationFormList) {
        Boolean result = locationService.batchSaveLocationDuringService(orderServiceLocationFormList);
        return Result.ok(result);
    }

    /**
     * 开始代驾服务后，定时获取订单服务期间司机的最新位置信息
     * @param orderId
     * @return
     */
    @Operation(summary = "开始代驾服务后，定时获取订单服务期间最新位置信息")
    @GetMapping("/getOrderServiceLastLocation/{orderId}")
    public Result<OrderServiceLastLocationVo> getOrderServiceLastLocation(@PathVariable("orderId") Long orderId) {
        OrderServiceLastLocationVo orderServiceLastLocationVo = locationService.getOrderServiceLastLocation(orderId);
        return Result.ok(orderServiceLastLocationVo);
    }

    /**
     * 计算订单实际距离
     * @param orderId
     * @return
     */
    @Operation(summary = "计算订单实际距离")
    @GetMapping("/calculateOrderRealDistance/{orderId}")
    public Result<BigDecimal> calculateOrderRealDistance(@PathVariable("orderId") Long orderId) {
        BigDecimal result = locationService.calculateOrderRealDistance(orderId);
        return Result.ok(result);
    }
}

